import count from '@dfeidao/atom-nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
// import { spaceid } from '../../atom/config';
interface Message {
	productid: string;
	role_no: string;
	search_field_val?: object;
	page_size: number;
	page_no: number;
}
interface Aa {
	search_field_val: object;
	productid: string;
	role_no: string;
	_id: string;
}
export default async function atom(msg: Message, action_id: string, session_id: string) {
	const systemid = '12';
	const { exec, prepare } = query(action_id, session_id, systemid);
	prepare('allot_object_type', ['_id', 'object_type_desc'], { productid: msg.productid, object_type_desc: { $like: '功能权限' } }, 200, 1, [], []);
	const [data1] = await exec<Aa>();
	const arr = new Array();

	if (data1.length > 0) {
		data1.forEach((v) => {
			arr.push(v._id);
		});
	}
	prepare('pub_role_allot', ['pk_val', 'search_field_val', 'object_type_id'], { object_type_id: { $in: arr }, productid: msg.productid, role_no: msg.role_no }, msg.page_size, msg.page_no, [], []);
	const [data2] = await exec<Aa>();
	const c = count(action_id, session_id, systemid);
	c.prepare('pub_role_allot', { object_type_id: { $in: arr }, productid: msg.productid, role_no: msg.role_no });
	const [t] = await c.exec();
	return { dataArray: data2, count: t };
}
